home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / etc / init.d / alsa-utils < prev    next >
Text File  |  2008-09-25  |  10KB  |  388 lines

  1. #!/bin/sh
  2. #
  3. # alsa-utils initscript
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides:          alsa-utils
  7. # Required-Start:    $remote_fs
  8. # Required-Stop:     $remote_fs
  9. # Default-Start:     S
  10. # Default-Stop:      0 6
  11. # Short-Description: Restore and store ALSA driver settings
  12. # Description:       This script stores and restores mixer levels on
  13. #                    shutdown and bootup.On sysv-rc systems: to
  14. #                    disable storing of mixer levels on shutdown,
  15. #                    remove /etc/rc[06].d/K50alsa-utils.  To disable
  16. #                    restoring of mixer levels on bootup, rename the
  17. #                    "S50alsa-utils" symbolic link in /etc/rcS.d/ to
  18. #                    "K50alsa-utils".
  19. ### END INIT INFO
  20.  
  21. # Don't use set -e; check exit status instead
  22.  
  23. # Exit silently if package is no longer installed
  24. [ -x /sbin/alsactl ] || exit 0
  25.  
  26. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  27. MYNAME=/etc/init.d/alsa-utils
  28.  
  29. . /lib/lsb/init-functions
  30.  
  31. # $1 EXITSTATUS
  32. # [$2 MESSAGE]
  33. log_action_end_msg_and_exit()
  34. {
  35.     log_action_end_msg "$1" ${2:+"$2"}
  36.     exit $1
  37. }
  38.  
  39. # $1 PROGRAM
  40. executable()
  41. {
  42.     # If which is not available then we must be running before
  43.     # /usr is mounted on a system that has which in /usr/bin/.
  44.     # Conclude that $1 is not executable.
  45.     [ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
  46.     which "$1" >/dev/null 2>&1
  47. }
  48.  
  49. # Wait for filesystems to show up
  50. while [ ! -d /usr/bin -o ! -d /var/lib/alsa ]; do
  51.     sleep 0.2
  52. done
  53.  
  54. executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
  55.  
  56. bugout() { echo "${MYNAME}: Programming error" >&2 ; exit 123 ; }
  57.  
  58. # $1 <card ID> | "all"
  59. restore_levels()
  60. {
  61.     [ -f /var/lib/alsa/asound.state ] || return 1
  62.     CARD="$1"
  63.     [ "$1" = all ] && CARD=""
  64.     # Assume that if alsactl prints a message on stderr
  65.     # then it failed somehow.  This works around the fact
  66.     # that alsactl doesn't return nonzero status when it
  67.     # can't restore settings for the card
  68.     if MSG="$(alsactl restore $CARD 2>&1 >/dev/null)" && [ ! "$MSG" ] ; then
  69.         return 0
  70.     else
  71.         # Retry with the "force" option.  This restores more levels
  72.         # but it results in much longer error messages.
  73.         alsactl -F restore $CARD >/dev/null 2>&1
  74.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl restore' failed with error message '$MSG'"
  75.         return 1
  76.     fi
  77. }
  78.  
  79. # $1 <card ID> | "all"
  80. store_levels()
  81. {
  82.     CARD="$1"
  83.     [ "$1" = all ] && CARD=""
  84.     if MSG="$(alsactl store $CARD 2>&1)" ; then
  85.         sleep 1
  86.         return 0
  87.     else
  88.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl store' failed with error message '$MSG'"
  89.         return 1
  90.     fi
  91. }
  92.  
  93. echo_card_indices()
  94. {
  95.     if [ -f /proc/asound/cards ] ; then
  96.         sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards
  97.     fi
  98. }
  99.  
  100. filter_amixer_output()
  101. {
  102.     sed \
  103.         -e '/Unable to find simple control/d' \
  104.         -e '/Unknown playback setup/d' \
  105.         -e '/^$/d'
  106. }
  107.  
  108. # The following functions try to set many controls.
  109. # No card has all the controls and so some of the attempts are bound to fail.
  110. # Because of this, the functions can't return useful status values.
  111.  
  112. # $1 <control>
  113. # $2 <level>
  114. # $CARDOPT
  115. unmute_and_set_level()
  116. {
  117.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  118.     amixer $CARDOPT -q set "$1" "$2" unmute 2>&1 | filter_amixer_output || :
  119.     return 0
  120. }
  121.  
  122. # $1 <control>
  123. # $CARDOPT
  124. mute_and_zero_level()
  125. {
  126.     { [ "$1" ] && [ "$CARDOPT" ] ; } || bugout
  127.     amixer $CARDOPT -q set "$1" "0%" mute 2>&1 | filter_amixer_output || :
  128.     return 0
  129. }
  130.  
  131. # $1 <control>
  132. # $2 "on" | "off"
  133. # $CARDOPT
  134. switch_control()
  135. {
  136.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  137.     amixer $CARDOPT -q set "$1" "$2" 2>&1 | filter_amixer_output || :
  138.     return 0
  139. }
  140.  
  141. # $1 <card ID>
  142. sanify_levels_on_card()
  143. {
  144.     CARDOPT="-c $1"
  145.  
  146.     unmute_and_set_level "Front" "80%"
  147.     unmute_and_set_level "Master" "80%"
  148.     unmute_and_set_level "Master Mono" "80%"   # See Bug#406047
  149.     unmute_and_set_level "Master Digital" "80%"   # E.g., cs4237B
  150.     unmute_and_set_level "Playback" "80%"
  151.     unmute_and_set_level "Headphone" "70%"
  152.     unmute_and_set_level "PCM" "80%"
  153.     unmute_and_set_level "PCM,1" "80%"   # E.g., ess1969
  154.     unmute_and_set_level "DAC" "80%"     # E.g., envy24, cs46xx
  155.     unmute_and_set_level "DAC,0" "80%"   # E.g., envy24
  156.     unmute_and_set_level "DAC,1" "80%"   # E.g., envy24
  157.     unmute_and_set_level "Synth" "80%"
  158.     unmute_and_set_level "CD" "80%"
  159.  
  160.     mute_and_zero_level "Mic"
  161.     mute_and_zero_level "IEC958"         # Ubuntu #19648
  162.  
  163.     # Intel P4P800-MX  (Ubuntu bug #5813)
  164.     switch_control "Master Playback Switch" on
  165.     switch_control "Master Surround" on
  166.  
  167.     # Trident/YMFPCI/emu10k1:
  168.     unmute_and_set_level "Wave" "80%"
  169.     unmute_and_set_level "Music" "80%"
  170.     unmute_and_set_level "AC97" "80%"
  171.  
  172.     # DRC:
  173.     unmute_and_set_level "Dynamic Range Compression" "80%"
  174.  
  175.     # Required for HDA Intel (hda-intel):
  176.     unmute_and_set_level "Front" "80%"
  177.  
  178.     # Required for SB Live 7.1/24-bit (ca0106):
  179.     unmute_and_set_level "Analog Front" "80%"
  180.  
  181.     # Required at least for Via 823x hardware on DFI K8M800-MLVF Motherboard with kernels 2.6.10-3/4 (see ubuntu #7286):
  182.     switch_control "IEC958 Capture Monitor" off
  183.  
  184.     # Required for hardware allowing toggles for AC97 through IEC958,
  185.     #  valid values are 0, 1, 2, 3. Needs to be set to 0 for PCM1.
  186.     unmute_and_set_level "IEC958 Playback AC97-SPSA" "0"
  187.  
  188.     # Required for newer Via hardware (see Ubuntu #31784)
  189.     unmute_and_set_level "VIA DXS,0" "80%"
  190.     unmute_and_set_level "VIA DXS,1" "80%"
  191.     unmute_and_set_level "VIA DXS,2" "80%"
  192.     unmute_and_set_level "VIA DXS,3" "80%"
  193.  
  194.     # Required on some notebooks with ICH4:
  195.     switch_control "Headphone Jack Sense" off
  196.     switch_control "Line Jack Sense" off
  197.  
  198.     # Some machines need one or more of these to be on;
  199.     # others need one or more of these to be off:
  200.     #
  201.     # switch_control "External Amplifier" on
  202.     switch_control "Audigy Analog/Digital Output Jack" on
  203.     switch_control "SB Live Analog/Digital Output Jack" on
  204.     
  205.     # D1984 -- Thinkpad T61/X61
  206.     switch_control "Speaker" on
  207.     switch_control "Headphone" on
  208.  
  209.     # HDA-Intel w/ "Digital" capture mixer (See Ubuntu #193823)
  210.     unmute_and_set_level "Digital" "80%"
  211.  
  212.     return 0
  213. }
  214.  
  215. # $1 <card ID> | "all"
  216. sanify_levels()
  217. {
  218.     TTSDML_RETURNSTATUS=0
  219.     case "$1" in
  220.       all)
  221.         for CARD in $(echo_card_indices) ; do
  222.             sanify_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  223.         done
  224.  
  225.         ;;
  226.       *)
  227.         sanify_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  228.         ;;
  229.     esac
  230.     mute_and_zero_levels_on_card "pcsp"
  231.     return $TTSDML_RETURNSTATUS
  232. }
  233.  
  234. # $1 <card ID>
  235. preinit_levels_on_card()
  236. {
  237.     CARDOPT="-c $1"
  238.  
  239.     # Silly dance to activate internal speakers by default on PowerMac
  240.     # Snapper and Tumbler
  241.     id=`cat /proc/asound/card$1/id 2>/dev/null`
  242.     if [ "$id" = "Snapper" -o "$id" = "Tumbler" ]; then
  243.         switch_control "Auto Mute" off
  244.         switch_control "PC Speaker" off
  245.         switch_control "Auto Mute" on
  246.     fi
  247. }
  248.  
  249. # $1 <card ID> | "all"
  250. preinit_levels()
  251. {
  252.     TTSDML_RETURNSTATUS=0
  253.     case "$1" in
  254.       all)
  255.         for CARD in $(echo_card_indices) ; do
  256.             preinit_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  257.         done
  258.         ;;
  259.       *)
  260.         preinit_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  261.         ;;
  262.     esac
  263.     return $TTSDML_RETURNSTATUS
  264. }
  265.  
  266. # $1 <card ID>
  267. mute_and_zero_levels_on_card()
  268. {
  269.     CARDOPT="-c $1"
  270.     for CTL in \
  271.         Master \
  272.         PCM \
  273.         Synth \
  274.         CD \
  275.         Line \
  276.         Mic \
  277.         "PCM,1" \
  278.         Wave \
  279.         Music \
  280.         AC97 \
  281.         "Master Digital" \
  282.         DAC \
  283.         "DAC,0" \
  284.         "DAC,1" \
  285.         Headphone \
  286.         Speaker \
  287.         Playback
  288.     do
  289.         mute_and_zero_level "$CTL"
  290.     done
  291. #    for CTL in \
  292. #        "Audigy Analog/Digital Output Jack" \
  293. #        "SB Live Analog/Digital Output Jack"
  294. #    do
  295. #        switch_control "$CTL" off
  296. #    done
  297.     return 0
  298. }
  299.  
  300. # $1 <card ID> | "all"
  301. mute_and_zero_levels()
  302. {
  303.     TTZML_RETURNSTATUS=0
  304.     case "$1" in
  305.       all)
  306.         for CARD in $(echo_card_indices) ; do
  307.             mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
  308.         done
  309.         ;;
  310.       *)
  311.         mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
  312.         ;;
  313.     esac
  314.     return $TTZML_RETURNSTATUS
  315. }
  316.  
  317.  
  318. # $1 <card ID> | "all"
  319. card_OK()
  320. {
  321.     [ "$1" ] || bugout
  322.     if [ "$1" = all ] ; then
  323.         [ -d /proc/asound ]
  324.         return $?
  325.     else
  326.         [ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
  327.         return $?
  328.     fi
  329. }
  330.  
  331. # If a card identifier is provided in $2 then regard it as an error
  332. # if that card is not present; otherwise don't regard it as an error.
  333.  
  334. case "$1" in
  335.   start)
  336.     EXITSTATUS=0
  337.     TARGET_CARD="$2"
  338.     case "$TARGET_CARD" in
  339.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Setting up ALSA" ;;
  340.     esac
  341.     if ! card_OK "$TARGET_CARD"; then
  342.         [ "$TARGET_CARD" = "all" ] && log_action_end_msg "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  343.         exit $?
  344.     fi
  345.     preinit_levels "$TARGET_CARD" || EXITSTATUS=1
  346.     if ! restore_levels "$TARGET_CARD" ; then
  347.         sanify_levels "$TARGET_CARD" || EXITSTATUS=1
  348.         restore_levels "$TARGET_CARD" >/dev/null 2>&1 || :
  349.     fi
  350.     [ "$TARGET_CARD" = "all" ] && log_action_end_msg_and_exit "$EXITSTATUS"
  351.     exit $EXITSTATUS
  352.     ;;
  353.   stop)
  354.     EXITSTATUS=0
  355.     TARGET_CARD="$2"
  356.     case "$TARGET_CARD" in
  357.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Shutting down ALSA" ;;
  358.       *) log_action_begin_msg "Shutting down ALSA card ${TARGET_CARD}" ;;
  359.     esac
  360.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  361.     store_levels "$TARGET_CARD" || EXITSTATUS=1
  362.     mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
  363.     log_action_end_msg_and_exit "$EXITSTATUS"
  364.     ;;
  365.   restart|force-reload)
  366.     EXITSTATUS=0
  367.     $0 stop || EXITSTATUS=1
  368.     $0 start || EXITSTATUS=1
  369.     exit $EXITSTATUS
  370.     ;;
  371.   reset)
  372.     TARGET_CARD="$2"
  373.     case "$TARGET_CARD" in
  374.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Resetting ALSA" ;;
  375.       *) log_action_begin_msg "Resetting ALSA card ${TARGET_CARD}" ;;
  376.     esac
  377.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  378.     preinit_levels "$TARGET_CARD"
  379.     sanify_levels "$TARGET_CARD"
  380.     log_action_end_msg_and_exit "$?"
  381.     ;;
  382.   *)
  383.     echo "Usage: $MYNAME {start [CARD]|stop [CARD]|restart [CARD]|reset [CARD]}" >&2
  384.     exit 3
  385.     ;;
  386. esac
  387.  
  388.